#ifndef NETWORKHELPER_H
#define NETWORKHELPER_H

#include <QObject>
#include <QNetworkAccessManager>//URL的上传管理
#include <QNetworkRequest>//HTTP的URL请求管理
#include <QNetworkReply>//网页回复数据触发信号的类
#include <QList>
#include <QEventLoop> //QEventLoop类提供一种进入和离开事件循环的方法
#include <QCryptographicHash>
#include <QDateTime>
#include "databasehelper.h"

class NetworkHelper : public QObject {
    Q_OBJECT
public:
    explicit NetworkHelper(QObject *parent = nullptr);

    // 搜索音乐
    void searchMusic(const QString& keyword);

    // 获取音乐播放地址
    void getMusicUrl(const QString& album_id, const QString& hash);

signals:
    // 搜索结果回调
    void searchFinished(const QList<SongInfo>& songs);

    // 音乐地址和歌词回调
    void musicUrlReady(const QString& playUrl, const QString& lyric);

    // 错误信息回调
    void errorOccurred(const QString& errorMsg);

private slots:
    // 网络回复处理
    void onNetworkReply(QNetworkReply* reply);

private:
    // JSON解析
    QList<SongInfo> parseSearchJson(const QByteArray& data);
    QString parseMusicJson(const QByteArray& data, QString& lyric);

    QNetworkAccessManager* networkManager;

    // 酷狗API地址
    const QString kugouSearchApi = "http://mobilecdn.kugou.com/api/v3/search/song";
    const QString kugouDownloadApi = "https://wwwapi.kugou.com/yy/index.php";

    // 酷狗授权参数
    const QString difid = "lspkkh3QK9P0iJupz0oTy5G";
    const QString mid = "1d3f79956b03cf950e869c8242be0cd5";
    const QString platid = "4";
    //const QString r="play/getdata";
   // const QString hash="%1";
    //const QString album_id="%2";
    const QString appid = "1014";
  const QString version = "9108";
  const QString key = "NVPh5oo715z5DIWAeQlhMDsWXXQV4hw";
};

#endif // NETWORKHELPER_H
